package com.weita.lightfood.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author IT_Monster at 2019/06/10 22:54.
 * 返回到前端的数据封装
 */
@ApiModel
public class Result<T> implements Serializable {
    /**
     * 状态码
     */
    @Setter
    @Getter
    @ApiModelProperty("状态码")
    private int code;

    /**
     * 返回消息
     */
    @Setter
    @Getter
    @ApiModelProperty("返回消息")
    private String message;

    /**
     * 返回数据
     */
    @Getter
    @Setter
    @ApiModelProperty("返回数据")
    private T data;

    private static int CODE_SUCCESS = 200;
    private static String MSG_SUCCESS = "success";
    private static int CODE_FAIL = 400;
    private static String MSG_FAIL = "fail";

    public Result(){}

    public Result(int code, String message){
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 接口访问成功
     * @param data
     * @return
     */
    public static Result success(Object data){
        return new Result(CODE_SUCCESS,MSG_SUCCESS,data);
    }


    /**
     * 接口访问失败
     * @return
     */
    public static Result fail(){
        return new Result(CODE_FAIL,MSG_FAIL);
    }

    public static Result fail(String msg){
        return new Result(CODE_FAIL ,msg);
    }

}
